package cn.xxstudy.compose.material

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.MaterialTheme.shapes
import androidx.compose.material.TabRowDefaults.Divider
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import cn.xxstudy.compose.ui.theme.ComposeDemoTheme
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.launch
import androidx.compose.ui.platform.LocalContext

/**
 * @author ：wuxinxi on 2021/3/4 .
 * @packages ：cn.xxstudy.compose.theme .
 * TODO:Material Design Theme、Widget
 */
class SimpleMaterialThemeActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            PreviewDefault()
        }
    }
}


@Preview
@Composable
fun PreviewDefault() {
    val context = LocalContext.current
    val themeState = remember { mutableStateOf(false) }
    val dialogState = remember { mutableStateOf(false) }
    ComposeDemoTheme(darkTheme = themeState.value) {
        LazyColumn(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
                .padding(horizontal = 5.dp)
                .background(if (themeState.value) Color.White else Color.Black),
            verticalArrangement = Arrangement.spacedBy(4.dp)
        ) {
            item {
                Spacer(modifier = Modifier.padding(vertical = 4.dp))
            }
            item {
                MyBtn({ themeState.value = !themeState.value }, "更换主题")
            }
            item { Divider(color = Color.Red) }
            item {
                Text(text = "底部导航：BottomNavigation", color = MaterialTheme.colors.primary)
            }
            item {
                BottomNavigationComponent()
            }
            item { Divider(color = Color.Red) }
            item {
                MyBtn(onClick = {
                    dialogState.value = true
                }, text = "AlertDialog")
            }
            item {
                if (dialogState.value) {
                    AlertDialog(
                        onDismissRequest = {
                            dialogState.value = false
                        },
                        title = { Text(text = "I'm Dialog") },
                        text = {
                            Text(text = "This Compose Dialog")
                        },
                        confirmButton = {
                            TextButton(onClick = {
                                Toast.makeText(context, "确认", Toast.LENGTH_SHORT).show()
                                dialogState.value = false
                            }) {
                                Text(text = "确认")
                            }
                        },
                        dismissButton = {
                            TextButton(onClick = {
                                Toast.makeText(context, "取消", Toast.LENGTH_SHORT).show()
                                dialogState.value = false
                            }) {
                                Text(text = "取消")
                            }
                        },
                    )
                }
            }
            item { Divider(color = Color.Red) }
            item {
                MyBtn(onClick = {
                    context.startActivity(
                        Intent(
                            context,
                            SimpleBackdropScaffoldActivity::class.java
                        )
                    )
                }, text = "BackdropScaffold")
            }
            item { Divider(color = Color.Red) }
            item { BottomAppBarComponent() }
            item {
                MyBtn(onClick = {
                    context.startActivity(Intent(context, SimpleBottomDrawerActivity::class.java))
                }, text = "BottomDrawable")
            }
            item {
                MyBtn(onClick = {
                    context.startActivity(
                        Intent(
                            context,
                            SimpleBottomSheetScaffoldActivity::class.java
                        )
                    )
                }, text = "BottomSheetScaffold")
            }
            item {
                MyBtn(onClick = {
                    context.startActivity(Intent(context, SimpleWidgetActivity::class.java))
                }, text = "常用组件的使用")
            }
            item {
                MyBtn(onClick = {
                    context.startActivity(Intent(context, SimpleListItemActivity::class.java))
                }, text = "ListItem的使用")
            }
            item {
                MyBtn(onClick = {
                    val intent =
                        Intent(context, SimpleBottomModalBottomSheetLayoutActivity::class.java)
                    intent.putExtra("modalType", 0)
                    context.startActivity(intent)
                }, text = "ModalBottomSheetLayout的使用")
            }
            item {
                MyBtn(onClick = {
                    val intent =
                        Intent(context, SimpleBottomModalBottomSheetLayoutActivity::class.java)
                    intent.putExtra("modalType", 1)
                    context.startActivity(intent)
                }, text = "ModalDrawer的使用")
            }
        }
    }
}


@Composable
fun BottomNavigationComponent() {
    var selectedItem = remember { mutableStateOf(0) }
    val items = listOf("聊天", "通讯录", "发现", "我")
    val icons =
        listOf(Icons.Filled.Home, Icons.Filled.Person, Icons.Filled.Search, Icons.Filled.Face)
    BottomNavigation {
        items.forEachIndexed { index, item ->
            BottomNavigationItem(
                selectedItem.value == index,
                onClick = { selectedItem.value = index },
                label = { Text(text = items[index]) },
                icon = { Icon(icons[index], contentDescription = icons[index].name) }
            )
        }
    }
}

@Composable
fun BottomAppBarComponent() {
    BottomAppBar {
        IconButton(onClick = { /*TODO*/ }) {
            Icon(Icons.Filled.Home, Icons.Default.Home.name)
        }
        Spacer(modifier = Modifier.weight(1f, true))
        IconButton(onClick = { /*TODO*/ }) {
            Icon(Icons.Filled.Person, Icons.Default.Person.name)
        }
        Spacer(modifier = Modifier.weight(1f, true))
        IconButton(onClick = { /*TODO*/ }) {
            Icon(Icons.Filled.Favorite, Icons.Default.Favorite.name)
        }
    }
}

@Composable
fun MyBtn(onClick: () -> Unit, text: String) {
    Button(
        onClick = onClick,
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(5.dp), contentPadding = PaddingValues(15.dp)
    ) {
        Text(text = text)
    }
}